package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.IntegralGoods;
import com.weipai.springboot.model.SpecialGoods;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.SpecialGoodsDao;
import com.weipai.springboot.module.service.SpecialGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author songmengqiang
 * @create 2018-08-28 18:48
 **/
@Service
public class SpecialGoodsServiceImpl implements SpecialGoodsService {

    @Autowired
    private SpecialGoodsDao specialGoodsDao;

    @Override
    public Response save(SpecialGoods specialGoods) {
        Response response = new Response();
//        IntegralGoods goods = specialGoodsDao.selectByGoodsId(specialGoods.getGoodsId(), specialGoods.getSpecialOfferId());
        SpecialGoods goods = specialGoodsDao.selectByGoodsIdAndspecialId(specialGoods.getGoodsId(), specialGoods.getSpecialOfferId());
        if(null==goods){
            if(1==specialGoodsDao.insertSpecialGoods(specialGoods)){
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }else{
            response.setCode(RetCode.VAR_HAVE);
//            response.put("code","401");
//            response.put("msg","此商品已是会场商品，请勿重复添加");
        }
//        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response updateSpecialGoods(SpecialGoods specialGoods) {
        Response response = new Response();
        specialGoodsDao.updateSpecialGoods(specialGoods);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response deleteSpecialGoods(Integer id) {
        Response response = new Response();
        specialGoodsDao.deleteSpecialGoods(id);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response batchDeleteSpecialGoods(Integer[] ids) {
        Response response = new Response();
        specialGoodsDao.batchDeleteSpecialGoods(ids);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response listSpecialGoods(String specialOfferId, Integer page, Integer size, String draw) {
        Response response = new Response();
        Integer totalNum = specialGoodsDao.specialGoodsCount(specialOfferId);
        List<Map<String, Object>> list = specialGoodsDao.listSpecialGoods(specialOfferId, page, size);
        response.setCode(RetCode.SUCCESS);
        PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, totalNum, draw);
        response.put("pageList", pageList);
        return response;
    }
}
